package com.haohope.framework.datasource;

import com.alibaba.druid.util.StringUtils;

public enum DBType {
	mysql("mysql"),oracle("oracle"),sqlserver("sqlserver");
	String value;
	DBType(){
		this.value = "mysql";
	}
	DBType(String value) {
		if(StringUtils.isEmpty(value)) {
			throw new IllegalArgumentException("not instance dbtype.");
		}
		this.value = value.trim().toLowerCase();
	}
	public boolean isMysql(){
		return DBType.mysql.value.equalsIgnoreCase(this.value);
	}
	public boolean isOracle(){
		return DBType.oracle.value.equalsIgnoreCase(this.value);
	}
	public boolean isSqlserver(){
		return DBType.sqlserver.value.equalsIgnoreCase(this.value);
	}
	public String createDriverClassName() {
		if(isMysql()) {
			return "com.mysql.jdbc.Driver";
		}else if(isOracle()) {
			return "oracle.jdbc.driver.OracleDriver";
		}else if(isSqlserver()) {
			return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		}else {
			return null;
		}
	}
	public String createUrl(String ip,Integer port,String schema,String urladdition) {
		if(StringUtils.isEmpty(ip) || 
				StringUtils.isEmpty(schema) ||
				null == port
				) {
			return null;
		}
		String url = "";
		if(isMysql()) {
			url = "jdbc:mysql://" + ip + ":" + port + "/" + schema;
		    if(StringUtils.isEmpty(urladdition)) {
		    	url += "?useUnicode=true&characterEncoding=utf8&useFastDateParsing=false&useSSL=false";
		    }else {
		    	url += "?"+urladdition;
		    }
		}else if(isSqlserver()) {
			url = "jdbc:sqlserver://" + ip + ":" + port + ";databaseName=" + schema;
		}else if(isOracle()) {
			url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + schema;
		}
		return url;
	}
}
